/**
 * @author yxw
 * date : 2013年11月18日 下午4:17:19 
 */
package com.tzinfo.tzt.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.domain.MyResultList;
import com.tzinfo.tzt.domain.NewhouseBuyInfo;
import com.tzinfo.tzt.util.DBHelper;
import com.tzinfo.tzt.util.JsonTools;

public class HouseTuanService {

	Context context;

	public HouseTuanService(Context context) {
		this.context = context;
	}

	public boolean deleteAll() {
		return DBHelper.getInstance(context)
				.deleteAll(AppDataConfig.TuanTables);
	}


	public boolean deleteNewhouseBuyInfoId(String id) {
		int flag = DBHelper.getInstance(context).deleteById(
				AppDataConfig.TuanTables, id);
		return flag > 0 ? true : false;
	}

	public List<NewhouseBuyInfo> getNewhouseBuyInfoList() {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.TuanTables, "time", "desc");
		List<NewhouseBuyInfo> list = new ArrayList<NewhouseBuyInfo>();
		
		while (cursor.moveToNext()) {
			NewhouseBuyInfo item=new NewhouseBuyInfo();
			item.setHouseId(cursor.getInt(cursor.getColumnIndex("_id")) + "");

			String pics=cursor.getString(cursor.getColumnIndex("pics"));
			String[] picss=pics.split(";;");
			List<String > picsList=new ArrayList<String>();
			for (int i = 0; i < picss.length; i++) {
				picsList.add(picss[i]);
			}
			item.setPics(picsList);
			item.setName(cursor.getString(cursor.getColumnIndex("name")));
			item.setSales(cursor.getString(cursor.getColumnIndex("sales")));
			item.setCaption(cursor.getString(cursor.getColumnIndex("caption")));
			boolean b=cursor.getInt(cursor.getColumnIndex("isGroup"))==1?true:false;
			item.setIsGroup(b);
			item.setSignCount(cursor.getString(cursor.getColumnIndex("signCount")));
			item.setRemainTime(cursor.getString(cursor.getColumnIndex("remainTime")));
			item.setIntroduce(cursor.getString(cursor.getColumnIndex("introduce")));
			item.setNotice(cursor.getString(cursor.getColumnIndex("notice")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
			list.add(item);
		}
		cursor.close();
		return list;
	}

	public NewhouseBuyInfo getNewhouseBuyInfoById(String houseid) {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.TuanTables, houseid);
		NewhouseBuyInfo item = null;
		if (cursor.moveToNext()) {
			item=new NewhouseBuyInfo();
			item.setHouseId(cursor.getInt(cursor.getColumnIndex("_id")) + "");

			String pics=cursor.getString(cursor.getColumnIndex("pics"));
			String[] picss=pics.split(";;");
			List<String > picsList=new ArrayList<String>();
			for (int i = 0; i < picss.length; i++) {
				picsList.add(picss[i]);
			}
			item.setPics(picsList);
			item.setName(cursor.getString(cursor.getColumnIndex("name")));
			item.setSales(cursor.getString(cursor.getColumnIndex("sales")));
			item.setCaption(cursor.getString(cursor.getColumnIndex("caption")));
			boolean b=cursor.getInt(cursor.getColumnIndex("isGroup"))==1?true:false;
			item.setIsGroup(b);
			item.setSignCount(cursor.getString(cursor.getColumnIndex("signCount")));
			item.setRemainTime(cursor.getString(cursor.getColumnIndex("remainTime")));
			item.setIntroduce(cursor.getString(cursor.getColumnIndex("introduce")));
			item.setNotice(cursor.getString(cursor.getColumnIndex("notice")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
		}
		cursor.close();
		return item;
	}

	public boolean saveNewhouseBuyInfoJson(NewhouseBuyInfo info) {
		ContentValues value = new ContentValues();
		value.put("_id", info.getHouseId());
		//特殊处理
		List<String> picsList = info.getPics();
		String pics="";
		for (int i = 0; i < picsList.size(); i++) {
			pics+=picsList.get(i)+";;";
		}
		if(picsList.size()>0){
			pics=pics.substring(0,pics.length()-2);
		}
		value.put("pics", pics);
		value.put("name", info.getName());
		value.put("sales", info.getSales());
		value.put("caption", info.getCaption());
		int n=1;
		if(!info.isIsGroup()){
			n=0;
		}
		value.put("isGroup", n);
		value.put("signCount", info.getSignCount());
		value.put("remainTime", info.getRemainTime());
		value.put("introduce", info.getIntroduce());
		value.put("notice", info.getNotice());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		value.put("time", df.format(new Date()));// 获取当前时间
		long flag = DBHelper.getInstance(context).save(
				AppDataConfig.TuanTables, value);
		return flag > 0 ? true : false;
	}

}
